﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToolwebAQ.database;
using Ultility;

namespace ToolwebAQ.adminaq
{
    public partial class AQLogin : System.Web.UI.Page
    {
        tool tl = new tool();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadloaidanhmuc();
                txtUserName.Attributes.Add("onKeyPress", "doClick('" + txtUserName.ClientID + "',event)");
                txtPassword.Attributes.Add("onKeyPress", "doClick('" + txtPassword.ClientID + "',event)");
            }
        }

        void loadloaidanhmuc()
        {
            aquariusDBDataContext db = new aquariusDBDataContext();
            if (Cache["danhmuckn"] == null)
            {
                var lisdanhmuc = db.sp_Tbl_cate_type_select_all().ToList();
                Cache.Insert("danhmuckn", lisdanhmuc, null, DateTime.MaxValue, TimeSpan.FromSeconds(1800));
                ddlLanguage.DataSource = Cache["danhmuckn"];
                ddlLanguage.DataTextField = "cate_type";
                ddlLanguage.DataValueField = "id_cate";
                ddlLanguage.DataBind();
                ddlLanguage.Items.Insert(0, new ListItem("Chọn danh mục", "0"));
            }
            else
            {
                ddlLanguage.DataSource = Cache["danhmuckn"];
                ddlLanguage.DataTextField = "cate_type";
                ddlLanguage.DataValueField = "id_cate";
                ddlLanguage.DataBind();
                ddlLanguage.Items.Insert(0, new ListItem("Chọn danh mục", "0"));
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            aquariusDBDataContext db=new aquariusDBDataContext();
            if (txtUserName.Value.Trim() != "" || txtPassword.Value.Trim() != "")
            {
                if (txtUserName.Value.Trim().ToLower() == Source.obj_A.ToLower())
                {
                    Session[Source.obj_Account] = Source.obj_A;
                    Response.Redirect("AQMainpageAdmin.aspx");

                }
                if (ddlLanguage.SelectedValue == "0")
                {
                    lblNotice.Text = "Bạn chưa chọn danh mục";
                    return;
                }
                else
                {
                    
                    var list = db.Sp_Tbl_Account_login(txtUserName.Value.Trim().ToLower(), tl.Encrypt(txtPassword.Value.Trim()), int.Parse(ddlLanguage.SelectedValue)).ToList();
                    if (list.Count() != 0)
                    {
                        foreach (var Itemuser in list)
                        {
                            if (Itemuser.Isused != true)
                            {
                                lblNotice.Text = "Tài khoản này đã bị khóa!";
                            }

                            else if (Itemuser.Isused == true)
                            {
                                Session[Source.obj_Account] = Itemuser.Accountname;
                                Session[Source.obj_role] = Itemuser.admin;
                                Session[Source.obj_Cateid] = ddlLanguage.SelectedValue.Trim();

                                HttpCookie cookie;

                                cookie = Request.Cookies["StoredURLFromLastSession"];
                                if (cookie == null)
                                {
                                    Response.Redirect("AQMainpageAdmin.aspx");

                                }
                                else
                                {
                                    String StoredURLFromLastSession = cookie.Value;

                                    Response.Redirect(StoredURLFromLastSession);
                                }
                            }
                            else
                            {
                                lblNotice.Text = "Username hoặc Password chưa đúng!";
                            }

                        }
                    }
                    else
                    {
                        lblNotice.Text = "Username hoặc Password không tồn tại!";
                    }
                }
            }
        }
    }
}